package com.jyb.qiyefuwu.view.activity.usercenter;


import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ActivityRecordListBinding;
import com.jyb.qiyefuwu.uitls.DialogFunction;
import com.jyb.qiyefuwu.uitls.DialogHelper;
import com.jyb.qiyefuwu.view.adpater.BillingRecordAdapter;
import com.jyb.qiyefuwu.viewmodel.DatabaseListModel;
import com.sgb.link.router.PathEnterpriseServices;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.customview.PhotoViewActivity;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.swgk.core.common.base.BaseViewModel.SHOW_LODING;


/**
 * 作者:陈方凯
 * 日期:2021/10/29
 * 说明:开票记录
 */
@Route(path = PathEnterpriseServices.BillingRecordActivity)
public class BillingRecordActivity extends BaseActivity<DatabaseListModel, ActivityRecordListBinding> {
    private BillingRecordAdapter mAdapter;
    private boolean isLodMore;
    private Intent intent;
    private String orderId;
    private boolean isGone;
    private String status;
    private List<String> urls = new ArrayList<>();

    @Override
    protected int layoutId() {
        return R.layout.activity_record_list;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(DatabaseListModel.class);
    }

    @Override
    protected void init() {
        orderId = getIntent().getStringExtra("oderId");
        isGone = getIntent().getBooleanExtra("isGone", false);
        status = getIntent().getStringExtra("status");
        mBinding.tvTitle.setText("开票记录");
        ((TextView)mBinding.errorView.findViewById(R.id.no_data_tv)).setText("暂无开票记录");
        ((TextView)mBinding.errorView.findViewById(R.id.no_data_tv)).setTextColor(Color.parseColor("#333333"));
        mBinding.backView.setOnClickListener(this);
        mBinding.tvCommit.setOnClickListener(this);
        mBinding.imgAddVoucher.setOnClickListener(this);
        mBinding.tvCommit.setVisibility(View.GONE);
        mBinding.imgAddVoucher.setVisibility(isGone ? View.GONE : View.VISIBLE);
        if (isGone == true || status.equals("5")) {
            mBinding.imgAddVoucher.setVisibility(View.GONE);
        } else if(isGone == false){
            mBinding.imgAddVoucher.setVisibility(View.VISIBLE);
        }
        initAdapter();
        initObserver();
    }

    private void initAdapter() {
        mAdapter = new BillingRecordAdapter(this, null, isGone,status);
        mAdapter.setItemSelect(false);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(this, 2));
        mBinding.rvList.setAdapter(mAdapter);
        mAdapter.onDelete(new BillingRecordAdapter.IDelete() {
            @Override
            public void delete(String id) {
                //TODO 删除操作
                isLodMore = false;
                DialogHelper.showDialog(BillingRecordActivity.this, "提示", "是否确认删除？",
                        "取消", "确定", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                mViewModel.delBilling(id);
                            }
                        });
            }

            @Override
            public void lookPdf(String path) {
                //TODO 点击跳转浏览器
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri content_url = Uri.parse(path);
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void lookImg(String path) {
                //TODO 查看图片
                urls.clear();
                urls.add(path);
                openPhotoView(0);
            }
        });
        mBinding.rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //TODO 刷新
                isLodMore = false;
                mViewModel.getBillingList(false, orderId);
            }

            @Override
            public void onLoadMore() {
                //TODO 加载更多
                isLodMore = true;
                mViewModel.getBillingList(true, orderId);
            }
        });
        mViewModel.getBillingList(isLodMore, orderId);
    }

    private void initObserver() {
        mViewModel.billingData.observe(this, listdata -> {
            if (isLodMore) {
                if (null != listdata && listdata.size() > 0) {
                    mBinding.rvList.loadMoreComplete();
                    mAdapter.addDatas(listdata);
                } else {
                    mBinding.rvList.setNoMore(true);
                }
                mBinding.rvList.setVisibility(View.VISIBLE);
            } else {
                mBinding.rvList.refreshComplete();
                if (listdata.size() == 0) {
                    mAdapter.setDatasMayNull(listdata);
                    mBinding.errorView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.errorView.setVisibility(View.GONE);
                    mBinding.rvList.setVisibility(View.VISIBLE);
                    mAdapter.setDatas(listdata);
                }
            }
        });
        mViewModel.delOk.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                //刷新数据
                isLodMore = false;
                mViewModel.getBillingList(false, orderId);
            }
        });
        mViewModel.delFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                //刷新数据
                isLodMore = false;
                mViewModel.getBillingList(false, orderId);
            }
        });
        mViewModel.toastMsg.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(BillingRecordActivity.this, s);
            }
        });
        mViewModel.mLoading.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer == SHOW_LODING) {
                    com.swgk.core.dialog.DialogHelper.showProgressDialog(BillingRecordActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                } else {
                    com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                }
            }
        });
    }

   /* @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.tv_commit) {
            DialogHelper.showDialog(this, "提示", "请前往PC端上传资料",
                    "", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });
        }
    }*/

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.tv_commit) {
            DialogHelper.showDialog(this, "提示", "确认提交全部凭证？提交后将不能继续上传。",
                    "取消", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });
        } else if (id == R.id.img_add_voucher) {
            DialogHelper.showDialog(this, "提示", "请前往PC端上传资料",
                    "", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {

                        }
                    });
        }
    }

    /**
     * 预览
     **/
    private void openPhotoView(int position) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setClass(this, PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
        this.startActivity(intent);
    }

}
